<template>
	<div>
		<!--负载均衡 转发 列表-->
		
		<listTable
		    v-loading="loading"
			ref="listTable"
			v-if="listOption" 
			:options="listOption"
			@command="listCommand"
			></listTable>
			
	</div>
</template>

<script>
import { zhuanfaStatusFilter } from '@/filters/status/index';
import listTable from "@/view/components/table/listTable";
import { apiUrls, getHostInfCluster } from "@/service/swagger/ResourceHostController"
import { getRuleById } from "@/service/swagger/HaproxyRuleController";
import { stop } from "@/service/swagger/HaproxyController";

export default {
	props: [
		"data",
		"ip",
	],
	components: {
		listTable,
	},
	data() {
		return {
			loading: false,
			listOption: {
				noPage: true,
				// 列表上部btn组
				listBtns: [{
					label: "添加",
					iconfont: "icon-folder-add",
					commandName: "addZJ",
				}],
				// 列表api
				//				listApi: {
				//					serviceFN: getHostInfCluster,
				//					//url : apiUrls.getHostInfCluster,
				//					//type : "POST",
				//					params: {
				//						clusterId: this.$route.query.id ? Number(this.$route.query.id) : 0,
				//					},
				//				},
				listData: [{}, {}],
				// 表头设置
				header: [{
					label: "IP",
					width: "",
					propName: "ip",
					align: "left",
					html: (row) => {
						return `<span class="blue">${row.ip || ''}</span>`
					},
					commandName: "goDetail",
				}, {
					label: "转发名称",
					propName: "name",
				}, {
					label: "代理端口",
					propName: "port",
				}, {
					label: "调度算法",
					html: (row) => {
						const hash = {
							roundrobin: "轮询",
							source: "哈希一致性",
						}
						return hash[row.balance] || "";
					}
				}, {
					label: "协议",
					propName: "protocol",
				}, {
					label: "创建时间",
					propName: "createTime",
				}, {
					label: "是否启用",
					html: (row) => {
						return zhuanfaStatusFilter(row.status);
					}
				}, {
					label: "操作",
					width: "",
					btns: [{
						label: "修改",
						commandName: "editZJ",
					}, {
						label: "删除",
						commandName: "deleteZJ",
					}],
					btnGroups: []
				}]
			}
		}
	},
	mounted() {
		// this.search();
		this.listOption.listData = this.data;
	},
	methods: {
		listCommand(command, row, index) {
			if (command && this[command]) {
				this[command](row, index);
			}
		},
		initData(_data) {
			this.$refs.listTable.serviceCallBack({
				data: _data
			})
		},
		//		search(params) {
		//			if (this.$refs.listTable) {
		//				this.$refs.listTable.search(params)
		//			}
		//		},


		addZJ(row, index) {
			this.$emit("command", "addZFPZ", row, index)
		},
		editZJ(row, index) {
			getRuleById({
				id: row.id,
			}).then(ret => {
				if (!ret.code) {
					this.$emit("command", "updateZFPZ", ret.data, index)
				}
			})
		},
		goDetail(row, index) {

		},
		deleteZJ(row, index) {
			this.$confirm(`您确认删除转发规则${row.name}？`, "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(() => {
				this.loading = true
				stop({
					id: row.id
				}).then(ret => {
					this.loading = false
					if (!ret.code) {
						this.$message({ message: "删除成功", type: "success" })
						this.$emit("command", "refresh", ret.data, index)
					}
				});
			});

		}

	}

}
</script>

<style>
</style>